import 'package:get/get.dart';
import 'package:xin_he/page/adopt/model/xin_he_adopt_tree_model_entity.dart';
import 'package:xin_he/page/adopt/model/xin_he_adopt_tree_record_model_entity.dart';

import '../../../service/xin_he_reqest.dart';
import '../../../utils/xin_he_event_bus.dart';
import '../../../utils/xin_he_storage.dart';
import '../../../utils/xin_he_toast.dart';
import 'xin_he_adopt_state.dart';

class XinHeAdoptLogic extends GetxController {
  final XinHeAdoptState state = XinHeAdoptState();

  RxList recordsList = [].obs;
  RxInt adoptCount = 0.obs;
  Rx<XinHeAdoptTreeModelEntity> treeModel = XinHeAdoptTreeModelEntity().obs;
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    requestAdoptCount();

    requestTreeInfo();

    bus.on(XinHeStorage.KNeedRefresh, (arg) async {

      if(arg == 2) {
        requestAdoptCount();

        requestTreeInfo();
      }

    });

  }




  ///认养数量
  requestAdoptCount() async{
    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);
    Map<String,dynamic> ss = {
      'userId':userId,
    };

    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/adoptedTreeOrder/adoptedTreeCount',params: ss,isPostAndwwwForm: true);
    if(!result.result || result.data['code'] != 0) {
      XinHeToast.center(result.data['message']);
      return ;
    }
    adoptCount.value = result.data['data'] ??0;

  }


  ///认养树信息
  requestTreeInfo() async{
    var userId = await XinHeLocalStorage.get(XinHeStorage.userId);

    Map<String,dynamic> ss = {
      'userId':userId,
    };

    XinHeRequestDataResult result = await XinHeHttpRequestData.requestUniversalPost(url: '/adoptedTree/getAdoptedTree',isPost: false);
    if(!result.result || result.data['code'] != 0) {
      XinHeToast.center(result.data['message']);
      return ;
    }

    XinHeAdoptTreeModelEntity model = XinHeAdoptTreeModelEntity.fromJson(result.data['data']);
    treeModel.value = model;
  }

}
